package com.letv.android.client.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.letv.core.db.PreferencesManager;
import com.letv.core.utils.LogInfo;
import com.media.ffmpeg.FFMpegPlayer;

import com.ledim.utils.ToastUtil;
import java.util.ArrayList;

public class PlayBlock {
    private static int BLOCK_DURATION = 2 * 1000;
    private static int BLOCK_CYCLE = 30 * 1000;

    private ArrayList<BlockEvent> mBlocks = new ArrayList<BlockEvent>(2);

    private Context mContext;
    private PlayBlockCallback mCallback;

    // 统计
    long blockStartTime = 0;// 单次卡顿开始时间戳
    long blockEndTime = 0;// 单次卡顿结束时间戳

    private Handler mHandler;

    public PlayBlock(Context context, PlayBlockCallback callback) {
        this.mContext = context;
        this.mCallback = callback;

        initData();
    }

    private void initData() {
        mHandler = new Handler(mContext.getMainLooper(), new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                mCallback.onBlock(null, FFMpegPlayer.MEDIA_BLOCK_END);
                return false;
            }
        });
    }

    /**
     * 测试卡顿流程
     */
    public void startTestBlock() {
        mCallback.onBlock(null, FFMpegPlayer.MEDIA_BLOCK_START);
        mHandler.sendEmptyMessageDelayed(0, 8 * 1000);
    }

    /**
     * 卡顿开始
     */
    public void blockStart() {
        blockStartTime = System.currentTimeMillis();
        LogInfo.LogStatistics("直播半屏-卡顿开始");
//        LiveStatisticsInfo.getInstance(mContext).updateBlockDataStatistics(true, 0, "");
    }

    /**
     * 卡顿结束
     */
    public void blockEnd() {
        blockEndTime = System.currentTimeMillis();
        if (blockEndTime - blockStartTime > BLOCK_DURATION) {// 卡顿超过两秒
            addBlockEvent();
        }
        LogInfo.LogStatistics("直播半屏-卡顿结束");
//        LiveStatisticsInfo.getInstance(mContext).updateBlockDataStatistics(false, blockEndTime - blockStartTime, "");
    }

    private void addBlockEvent() {
        BlockEvent event = new BlockEvent(blockStartTime, blockEndTime);
        if (mBlocks.size() == 0) {// 第一次卡顿
            mBlocks.add(event);
            if (PreferencesManager.getInstance().isTestApi()) {
                ToastUtil.toast("test msg : 30s内第一次卡顿，卡顿持续时间 = " + (blockEndTime - blockStartTime)
                        + " ms");
            }
        } else if (mBlocks.size() == 1) {// 第二次卡顿
            if (event.startTime - mBlocks.get(0).endTime <= BLOCK_CYCLE) {// 两次卡顿不超过30秒
                mCallback.blockTwiceAlert();
                if (PreferencesManager.getInstance().isTestApi()) {
                    ToastUtil.toast("30s内第二次卡顿，卡顿持续时间 = " + (blockEndTime - blockStartTime)
                            + " ms, 本地卡顿开始距离上次卡顿结束时间为" + (event.startTime - mBlocks.get(0).endTime) + " ms");
                }
                mBlocks.clear();
            } else {// 上次卡顿结束距离本次卡顿开始超过了30秒
                mBlocks.clear();
                mBlocks.add(event);
            }
        }
    }

    class BlockEvent {
        // 卡顿结束时间
        public long endTime;
        public long startTime;

        public BlockEvent(long startTime, long endTime) {
            this.startTime = startTime;
            this.endTime = endTime;
        }
    }

    public interface PlayBlockCallback {

        void onBlock(FFMpegPlayer ffMpegPlayer, int blockInfo);

        void blockTwiceAlert();
    }
}
